package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Rol;

@Repository(value = "rolDao")
@Transactional(readOnly = false)
public class RolDaoImpl implements RolDao {

	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	} 
	
	@SuppressWarnings("unchecked")
	public List<Rol> getRolList() {
		return em.createNamedQuery("Rol.findAll").getResultList();
	}

	public Rol findByRolId(Short idRol) {
		Rol res=null;
		try {
			res=em.find(Rol.class,idRol);
		} catch (Exception e) {
			// TODO: handle exception
		}
    	return res;
	}

	public void update(Rol rol) {
		em.merge(rol);
	}

	public void remove(Rol rol) {
		em.remove(em.getReference(Rol.class,rol.getRolId()));
	}

	public void add(Rol rol) {
		em.persist(rol);
	}

}
